package com.xw.controller.xw;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xw.controller.base.BaseController;
import com.xw.entity.Page;
import com.xw.entity.system.User;
import com.xw.entity.xw.Drug;
import com.xw.entity.xw.Inandout;
import com.xw.entity.xw.Kc;
import com.xw.service.xw.ck.CkService;
import com.xw.service.xw.drug.DrugService;
import com.xw.util.Const;
import com.xw.util.PageData;

/**
 * 出库
 * @author jeff
 *
 */

@Controller
@RequestMapping(value = "/ck")
public class CkController extends BaseController{
	
	@Resource(name = "ckService")
	private CkService ckService;
	@Resource(name = "drugService")
	private DrugService drugService;
	
	/**
	 * 获取药品入库列表
	 * @throws Exception 
	 * 
	 * 
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(Page page) throws Exception{
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData(); 
		pd = this.getPageData();
		String gjz = pd.getString("gjz");
		String beginTime = pd.getString("beginTime");
		if (beginTime != null && !"".equals(beginTime)) {
			beginTime = beginTime + " 00:00:00";
			pd.put("beginTime", beginTime);
		}
		String endTime = pd.getString("endTime");
		if (endTime != null && !"".equals(endTime)) {
			endTime = endTime + " 00:00:00";
			pd.put("endTime", endTime);
		}
		pd.put("gjz", gjz);
		page.setPd(pd);	
		List<PageData> listAll  =  ckService.getList(page);
		mv.addObject("listAll", listAll);
		mv.setViewName("xw/ck/ck_list");
		return mv;
	}
	
	
	
	/**
	 * 添加入库界面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goAdd")
	public ModelAndView goAdd() throws Exception{
		ModelAndView mv = new ModelAndView();
		
		mv.setViewName("xw/ck/addck");
		return mv;
	}
	
	
	/**
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/list2")
	public ModelAndView list2(Page page) throws Exception{
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		String gjz = pd.getString("gjz");
		
		Subject currentUser = SecurityUtils.getSubject();//用户Id,session里面获取
		Session session = currentUser.getSession();
		User user = new User();
		user = (User)session.getAttribute(Const.SESSION_USER);
		long mzId =user.getMzid();
		pd.put("mzId",mzId);
		System.out.println("门诊id门诊id门诊id门诊id门诊id："+mzId);
		pd.put("gjz",gjz);
		page.setPd(pd);
		List<PageData> listAll  =  drugService.getList(page);
		mv.addObject("listAll", listAll);
		mv.setViewName("xw/ck/addck");
		return mv;
	}
	
	
	
	/**
	 * 
	 * @param response
	 * @param session
	 * @throws Exception 
	 */
	@RequestMapping(value="/add")
	public ModelAndView add() throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String ypId = pd.getString("id");
		String inCount = pd.getString("rksl");
		System.out.println("idididdid:"+ypId);
		System.out.println("rkslrkslrksl:"+inCount);
		Drug drug=drugService.getOne(pd);
		Inandout inandout = new Inandout();
		inandout.setBs("0");//1表示入库，0表示出库
		//inandout.setCode("rk001");//java生成单号类
		
		Subject currentUser = SecurityUtils.getSubject();//这是用户Id,session里面获取
		Session session = currentUser.getSession();
		User user = new User();
		user = (User)session.getAttribute(Const.SESSION_USER);		
		String userId=(String) user.getUSER_ID();
		inandout.setCzrId(userId);//这是用户Id,session里面获取
		System.out.println("门诊id："+user.getMzid());
		inandout.setMzId(user.getMzid());//这个从session里可以获取门诊Id
		
		inandout.setInCount(new BigDecimal(inCount));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		Date time= sdf.parse(sdf.format(new Date()));
		inandout.setInDate(time);		
		inandout.setPrice(drug.getPrice());
		inandout.setYpCode(drug.getYpCode());
		inandout.setYpId(Long.valueOf(ypId));//药品Id
		inandout.setYpName(drug.getYpName());
		inandout.setYpSpell(drug.getYpSpell());
		inandout.setYpType(drug.getYpType());
		inandout.setYpUnit(drug.getYpUnit());
		inandout.setYpUsage(drug.getYpUsage());
		long mzId = inandout.getMzId();
		pd.put("mzId",mzId);
		pd.put("ypId",ypId);
		Kc kc2 = ckService.getKcByMzidAndYpid(pd);
		BigDecimal ykc = kc2.getKcTotal();
		BigDecimal rks = new BigDecimal(inCount);
		BigDecimal kcTotal = ykc.subtract(rks);
		System.out.println("总库存："+kcTotal);
		kc2.setKcTotal(kcTotal);
		ckService.editkc(kc2);
		ckService.add(inandout);
		
		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;	
	}
	
	
	/**
	 * 删除药品入库信息
	 * @param response
	 * @param session
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value="/delete")
	public void delete(HttpServletRequest request) throws Exception{
		PageData pd = this.getPageData();
		String id = request.getParameter("id");
		
		pd.put("id",id);
		Inandout inandout = ckService.getOne(pd);
		BigDecimal rks = inandout.getInCount();
		long mzId = inandout.getMzId();
		long ypId = inandout.getYpId();
		pd.put("mzId",mzId);
		pd.put("ypId",ypId);
		Kc kc2 = ckService.getKcByMzidAndYpid(pd);
		BigDecimal ykc = kc2.getKcTotal();
		BigDecimal kcTotal = ykc.add(rks);
		kc2.setKcTotal(kcTotal);
		System.out.println("总库存总库存总库存："+kcTotal);
		ckService.editkc(kc2);
		ckService.delete(pd);
	}
	
	
	/**
	 * 
	 * @param session
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(value = "/goEdit")
	public ModelAndView goEdit(HttpServletRequest request) throws Exception{
		ModelAndView mv = new ModelAndView();
		PageData pd =  this.getPageData();
		
		String id = request.getParameter("id");
		id = URLDecoder.decode(id, "UTF-8");
		pd.put("id", id);
		System.out.println("ididididid:"+id);
		
		Subject currentUser = SecurityUtils.getSubject();//这是用户Id,session里面获取
		Session session = currentUser.getSession();
		User user = new User();
		user = (User)session.getAttribute(Const.SESSION_USER);		
		String userName=user.getNAME();
				
		Inandout inandout = ckService.getOne(pd);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String inDate=sdf.format(inandout.getInDate());
		mv.addObject("inDate",inDate);		
		mv.addObject("inandout",inandout);
		mv.addObject("userName",userName);
		mv.setViewName("xw/ck/ckxq");
		return mv;
	}
	
}
